home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / REALITY / fastshadows / glshade.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.1 KB  |  212 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    glshade -
  19.  *        Simple support for describing materials and light sources 
  20.  *    This makes it easy for people without PHD's in GL to actually 
  21.  *    shade surfaces.
  22.  *
  23.  *                Paul Haeberli - 1990
  24.  *
  25.  *  exports:
  26.  *        matrixinit();
  27.  *        shadeinit();
  28.  *        shadeon();
  29.  *        shadeoff();
  30.  *        setdiffuse(r,g,b);
  31.  *        setspecular(r,g,b);
  32.  *        setshininess(s);
  33.  *
  34.  *
  35.  *  Here is the structure of a typical program:
  36.  *
  37.  *        main() 
  38.  *        {
  39.  *        winopen("shade");
  40.  *        RGBmode();
  41.  *        doublebuffer();
  42.  *        gconfig();
  43.  *        winopen("blat");
  44.  *        matrixinit();            you must use two matrix model 
  45.  *        .
  46.  *        .
  47.  *        perspective(  . . . . );
  48.  *        shadeinit();            init shading after perspective 
  49.  *        .
  50.  *        .
  51.  *        while(1)
  52.  *            cpack(0x00404060);        clear the screen
  53.  *            clear();
  54.  *            zclear();
  55.  *            .
  56.  *            .
  57.  *            shadeon();            turn lighting on
  58.  *                setdiffuse(1.0,0.0,0.0);    make the diffuse color red
  59.  *            drawobjwithnormals();    draw the object
  60.  *            shadeoff();            turn lighting off
  61.  *            .
  62.  *            .
  63.  *            swapbuffers();        swap buffers
  64.  *         }
  65.  *        }
  66.  */
  67. #include "stdio.h"
  68. #include "gl.h"
  69.  
  70. static initmaterial();
  71. static initlights();
  72. static makelight();
  73.  
  74. static float   idmat[4][4] = {
  75.     1.0, 0.0, 0.0, 0.0,
  76.     0.0, 1.0, 0.0, 0.0,
  77.     0.0, 0.0, 1.0, 0.0,
  78.     0.0, 0.0, 0.0, 1.0
  79. };
  80.  
  81. matrixinit()
  82. {
  83.     mmode(MVIEWING); 
  84.     loadmatrix(idmat);
  85. }
  86.  
  87. shadeinit()
  88. {
  89.     initmaterial();
  90.     initlights();
  91.     shadeoff();
  92. }
  93.  
  94. shadeon()
  95. {
  96.     lmbind(LMODEL,1);
  97. }
  98.  
  99. shadeoff()
  100. {
  101.     lmbind(LMODEL,0);
  102. }
  103.  
  104. setdiffuse(r,g,b)
  105. float r, g, b;
  106. {
  107.     float c[3];
  108.  
  109.     lmcolor(LMC_DIFFUSE);
  110.     c[0] = r;
  111.     c[1] = g;
  112.     c[2] = b;
  113.     c3f(c);
  114.     lmcolor(LMC_COLOR);
  115. }
  116.  
  117. setspecular(r,g,b)
  118. float r, g, b;
  119. {
  120.     float c[3];
  121.  
  122.     lmcolor(LMC_SPECULAR);
  123.     c[0] = r;
  124.     c[1] = g;
  125.     c[2] = b;
  126.     c3f(c);
  127.     lmcolor(LMC_COLOR);
  128. }
  129.  
  130. setshininess(s)
  131. float s;
  132. {
  133.     float mat_desc[3];
  134.  
  135.     mat_desc[0] = SHININESS;
  136.     mat_desc[1] = s;
  137.     mat_desc[2] = LMNULL;
  138.     lmdef(DEFMATERIAL,1,3,mat_desc);
  139. }
  140.  
  141.  
  142. static initmaterial()
  143. {
  144.     float mat_desc[19];
  145.  
  146.     mat_desc[0] = EMISSION;
  147.     mat_desc[1] = 0.0;
  148.     mat_desc[2] = 0.0;
  149.     mat_desc[3] = 0.0;
  150.     mat_desc[4] = AMBIENT;
  151.     mat_desc[5] = 0.0;
  152.     mat_desc[6] = 0.0;
  153.     mat_desc[7] = 0.0;
  154.     mat_desc[8] = DIFFUSE;
  155.     mat_desc[9] = 1.0;
  156.     mat_desc[10] = 1.0;
  157.     mat_desc[11] = 1.0;
  158.     mat_desc[12] = SPECULAR;
  159.     mat_desc[13] = 0.9;
  160.     mat_desc[14] = 0.9;
  161.     mat_desc[15] = 0.9;
  162.     mat_desc[16] = SHININESS;
  163.     mat_desc[17] = 40.0;
  164.     mat_desc[18] = LMNULL;
  165.     lmdef(DEFMATERIAL,1,19,mat_desc);
  166.     lmbind(MATERIAL,1);
  167. }
  168.  
  169. static initlights()
  170. {
  171.     float li_desc[10];
  172.  
  173.     li_desc[0] = AMBIENT;
  174.     li_desc[1] = 0.10;
  175.     li_desc[2] = 0.10;
  176.     li_desc[3] = 0.10;
  177.     li_desc[4] = LOCALVIEWER;
  178.     li_desc[5] = 0.0;
  179.     li_desc[6] = ATTENUATION;
  180.     li_desc[7] = 1.0;
  181.     li_desc[8] = 0.0;
  182.     li_desc[9] = LMNULL;
  183.     lmdef(DEFLMODEL,1,10,li_desc);
  184.     lmbind(LMODEL,1);
  185.  
  186.     makelight(0,-3.0,3.0,10.0);
  187. }
  188.  
  189. static makelight(i,x,y,z)
  190. int i;
  191. float x, y, z;
  192. {
  193.     float li_desc[14];
  194.  
  195.     li_desc[0] = AMBIENT; 
  196.     li_desc[1] = 0.1; 
  197.     li_desc[2] = 0.1;
  198.     li_desc[3] = 0.1;
  199.     li_desc[4] = LCOLOR;
  200.     li_desc[5] = 1.0;
  201.     li_desc[6] = 1.0;
  202.     li_desc[7] = 1.0;
  203.     li_desc[8] = POSITION;
  204.     li_desc[9] = x;
  205.     li_desc[10] = y;
  206.     li_desc[11] = z;
  207.     li_desc[12] = 0.0;        /* zero means infinte light */
  208.     li_desc[13] = LMNULL;
  209.     lmdef(DEFLIGHT,i+1,14,li_desc);
  210.     lmbind(LIGHT0+i,i+1);
  211. }
  212.